package com.example.zhujingmin.alarmservice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.view.View.OnClickListener;
import java.util.Calendar;


public class Alarmservice extends AppCompatActivity implements OnClickListener {
    private static Alarmservice appREF =null;
    private Button b_call_service, b_exit_service;
    boolean k =false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appREF = this;
        setContentView(R.layout.activity_alarmservice);
        b_call_service=(Button)findViewById(R.id.button);
        b_call_service.setOnClickListener(this);
        b_exit_service=(Button)findViewById(R.id.button2);
        b_exit_service.setOnClickListener(this);
    }
    public static Alarmservice getApp(){
        return  appREF;
    }
    public void btEvent(String data){
        setTitle(data);
    }
    public void onClick(View arg0) {
        if(arg0==b_call_service){
            setTitle("Waiting...");
            Intent intent =new Intent(Alarmservice.this,AlarmRecevier.class);
            PendingIntent p_intent=PendingIntent.getBroadcast(
                    Alarmservice.this,0,intent,0);
            Calendar calendar =Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND,3);
            AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),p_intent);
        }
if (arg0 ==b_exit_service){
    Intent intent=new Intent(Alarmservice.this,AlarmRecevier.class);
    PendingIntent p_intent= PendingIntent.getBroadcast(Alarmservice.this,0,intent,0);
    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
    am.cancel(p_intent);
    finish();
}
    }
}
